#include "conectionwindow.h"

ConnectionWindow::ConnectionWindow(QWidget *parent) :
    QWidget(parent)
{
    db = QSqlDatabase::addDatabase("QMYSQL");
    QPixmap pix_on_but(":/icons/play(1).png");
    connect_but = new PushButton(this);
    connect_but->setFlat(true);
    connect_but->setIcon(QIcon(pix_on_but));
    connect_but->setFixedSize(pix_on_but.size());
    connect_but->setIconSize(connect_but->size());
    connect_but->setToolTip(QString::fromLocal8Bit("встановити з'єднання"));
    connect_but->setEnabled(false);
    connect(connect_but, SIGNAL(clicked()), SLOT(connectToServer()));
    addr_server_edit = new LineEdit(this);
    addr_server_edit->setObjectName("hosts");
    connect(addr_server_edit, SIGNAL(textChanged(QString)), SLOT(insertingText(QString)));
    connect(addr_server_edit, SIGNAL(textChanged(QString)), SLOT(getHistoryTree(QString)));
    QLabel *descr_addr = new QLabel(QString::fromLocal8Bit("ip-адреса сервера:"), this);
    QLabel *descr_login = new QLabel(QString::fromLocal8Bit("логін:"), this);
    login_edit = new LineEdit(this);
    login_edit->setObjectName("users");
    connect(login_edit, SIGNAL(textChanged(QString)), SLOT(insertingText(QString)));
    connect(login_edit, SIGNAL(textChanged(QString)), SLOT(getHistoryTree(QString)));
    QLabel *descr_password = new QLabel(QString::fromLocal8Bit("пароль:"), this);
    password_edit = new LineEdit(this);
    password_edit->setEchoMode(QLineEdit::Password);
    history_list_wid = new QListWidget(this);
    history_list_wid->hide();
    connect(addr_server_edit, SIGNAL(focusOutEventSig()), history_list_wid, SLOT(hide()));
    connect(login_edit, SIGNAL(focusOutEventSig()), history_list_wid, SLOT(hide()));
    connect(login_edit, SIGNAL(pressKeyDown()), history_list_wid, SLOT(setFocus()));
    connect(history_list_wid, SIGNAL(itemClicked(QListWidgetItem*)), SLOT(historyItemSelected(QListWidgetItem*)));
    QGridLayout *grid_layout = new QGridLayout;
    grid_layout->addWidget(descr_addr, 0, 0);
    grid_layout->addWidget(addr_server_edit, 0, 1);
    grid_layout->addWidget(descr_login, 1, 0);
    grid_layout->addWidget(login_edit, 1, 1);
    grid_layout->addWidget(descr_password, 2, 0);
    grid_layout->addWidget(password_edit, 2, 1);
    grid_layout->addWidget(connect_but, 2, 2);
    setLayout(grid_layout);
}

ConnectionWindow::~ConnectionWindow()
{
    delete history_list_wid;
    std::transform(string_list_pointers.begin(), string_list_pointers.end(), string_list_pointers.begin(), __deletePointers);
}

QStringList *ConnectionWindow::__deletePointers(QStringList *p)
{
    delete p;
    p = 0;
    return p;
}

void ConnectionWindow::getHistoryTree(const QString &str)
{
    QLineEdit *tmp = dynamic_cast<QLineEdit*>(sender());
    history_list_wid->move(tmp->pos().x(), tmp->pos().y() + tmp->height());
    history_list_wid->clear();
    QStringList *hist_list =  history_tree.getHistoryTree(str, tmp->objectName());
    string_list_pointers.push_back(hist_list);
    if (!hist_list)
    {
        history_list_wid->hide();
        return; // раніше не вводлися подібні літери
    }
    history_list_wid->show();
    history_list_wid->resize(history_list_wid->width(), hist_list->size() * 15 + password_edit->height());
    history_list_wid->addItems(*hist_list);
}

void ConnectionWindow::historyItemSelected(QListWidgetItem *item)
{
    if (history_list_wid->objectName() == login_edit->objectName())
        login_edit->setText(item->text());
    else
        addr_server_edit->setText(item->text());
    history_list_wid->hide();
}

void ConnectionWindow::connectToServer()
{
    //history_tree.addHostsData(addr_server_edit->text());
    //history_tree.addUsersData(login_edit->text());
    db.setHostName(addr_server_edit->text());
    db.setDatabaseName("radius"); // FreeRadius всі свої таблиці розміщує в базі даних radius
    db.setUserName(login_edit->text());
    db.setPassword(password_edit->text());
    if (!db.open())
    {
        QMessageBox::critical(this, QString::fromLocal8Bit("Error..."), QString::fromLocal8Bit("помилка з'єднання!"));
        emit connectionNotOk();
    }
    else
    {
        login_edit->clear();
        addr_server_edit->clear();
        password_edit->clear();
        connect_but->setFlat(true);
        connect_but->setEnabled(false);
        emit connectionOk();
        hide();
    }
}

void ConnectionWindow::insertingText(const QString &str)
{
    QLineEdit *tmp = dynamic_cast<QLineEdit*>(sender());
    if (addr_server_edit->text().size() && login_edit->text().size()) connect_but->setEnabled(true);
    else
    {
        connect_but->setFlat(true);
        connect_but->setEnabled(false);
    }
    if (history_list_wid->objectName() != tmp->objectName()) history_list_wid->setObjectName(tmp->objectName());
}
